package biz.inv.service;


import biz.inv.dto.Balance;
import biz.inv.dto.StockIn;
import biz.inv.dto.StockInDetail;
import biz.inv.dto.StockTransSearch;
import glen.util.TemplateDAO;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class StockInSvr extends TemplateDAO<StockIn, Integer> {

    public StockInSvr() {
        super(StockIn.class);

    }

    public StockInSvr(Class<StockIn> type) {
        super(type);

    }

    public StockInSvr(Class<StockIn> type, SqlSessionFactory sf) {
        super(type, sf);

    }

    public List<StockIn> search(StockTransSearch o) throws PersistenceException {

        List<StockIn> list;

        try (SqlSession session = sf.openSession()) {
            String query = "StockIn.search";

            list = session.selectList(query, o);

        }
        return list;
    }

    public int delete(int id) {
        try (SqlSession session = sf.openSession()) {
            //StockIn stockIn=(StockIn)session.selectOne("StockIn.get", id);
            List<StockInDetail> details = session.selectList("StockIn.searchStockInDetail", id);
            for (StockInDetail detail : details) {
                session.update("Balance.deleteStockDetail", detail);
            }
            session.delete("StockIn.delete", id);
            session.delete("StockIn.deleteStockInDetail", id);

            session.commit();
        }
        return 0;
    }

    public int save(StockIn stockIn, List<StockInDetail> stockInDetails) {
        boolean isUpdate = (stockIn.getId() == 0);
        try (SqlSession session = sf.openSession()) {
            if (isUpdate) {
                session.insert("StockIn.create", stockIn);
            } else {
                session.update("StockIn.update", stockIn);
                //update Balance by sub Old StockIn Detail Qty
                List<StockInDetail> oldDetails = session.selectList("StockIn.searchStockInDetail", stockIn.getId());
                for (StockInDetail detail : oldDetails) {
                    session.update("Balance.update", new Balance(detail.getProduct().getId(), detail.getQty() * (-1)));
                }
                session.delete("StockIn.deleteStockInDetail", stockIn.getId());
            }

            for (StockInDetail detail : stockInDetails) {

                detail.setReceiveId(stockIn.getId());
                session.insert("StockIn.createStockInDetail", detail);

                Balance balance = session.selectOne("Balance.get", detail.getProduct().getId()); //Check Existing Balance

                if (balance == null) {
                    balance = new Balance(detail.getProduct().getId(), detail.getQty());
                    session.insert("Balance.create", balance);
                } else {
                    balance.setQty(detail.getQty());
                    session.update("Balance.update", balance);
                }
            }

            session.commit();
        }
        return 0;
    }

    public List<StockInDetail> searchStockInDetail(int id) {
        List<StockInDetail> list;
        try (SqlSession session = sf.openSession()) {
            list = session.selectList("StockIn.searchStockInDetail", id);
        }
        return list;
    }

}
